package com.my.screenadapter.MyRecyclerHeader.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by liuyafei on 2016/8/24.
 */
public abstract class BaseAdapter<T> extends RecyclerView.Adapter {
    protected List<T> datas;
    protected Context context;

    public BaseAdapter(Context context) {
        this.context = context;
    }


    /**
     * 设置数据
     *
     * @param datas
     */
    public void setDatas(List<T> datas) {
        this.datas = datas;
        //更新数据
        notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //设置参数
        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(5, 5, 5, 5);
        //获取布局
        View view = initView();
        view.setLayoutParams(params);
        return getBaseViewHolder(view);
    }

    protected abstract ItemViewHolder getBaseViewHolder(View view);

    /**
     * 初始化布局的方法
     *
     * @return
     */
    public abstract View initView();

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        //获取数据
        T data = datas.get(position);
        //获取holder对象
        ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        holder.itemView.setTag(position);
        //绑定数据
        itemViewHolder.bindData(data);
        final int finalPosition = position;
        ((ItemViewHolder) holder).holderView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemClick(v, finalPosition);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return datas != null ? datas.size() : 0;
    }

    private onItemClickedListener mListener;

    /**
     * 接口实现条目点击事件
     */
    public interface onItemClickedListener {
        abstract void onItemClick(View view, int position);
    }

    public void setOnItemClickedListener(onItemClickedListener mListener) {
        this.mListener = mListener;
    }


    protected abstract class ItemViewHolder extends RecyclerView.ViewHolder {
        //每一个条目
        public View holderView;

        public ItemViewHolder(View itemView) {
            super(itemView);
            holderView = itemView;
            createView();
        }

        /**
         * 初始化子控件
         */
        protected abstract void createView();

        /**
         * 绑定数据的方法
         *
         * @param t
         */
        protected abstract void bindData(T t);

    }

}
